如同第一天所說,基本語法的練習實在是太無聊了。不如就馬上來實作,從做中學吧₍₍ ◝(●˙꒳˙●)◜ ₎₎
To-do list應該就是任何應用程式上手的起手式ㄌㄅ!
因此這篇參考的資源如下
YouTube: Build To Do List App in Swift 5 (Xcode 12) - 2021 Beginners
Medium: Swift — 做個 To-Do List 吧( 基礎篇 )
透過 CocoaPods,開發者可以輕鬆地管理 Swift 或 Objective-C 的程式專案。
若果沒有相依性管理工具協助下,使用類庫便需要逐一下載並加入Xcode項目中,但當專案規模增大,要處理的類庫數目增加,逐一手動新增或更新絕對是費時失事之舉。開發者透過相依性管理工具(如 CocoaPods)幫助輕鬆管理Xcode項目,只需要透過簡單的指令就能管理類庫項目,節省大量配置和部署的時間。
就我的理解,像是node.js環境當中的npm,可以用來引入資料庫、並加以管理。
首先開啟一個新專案,此時在專案路徑底下打開終端機並初始化cocoapods。
pod init
open Podfile
打上上圖的指令pod 'RealmSwift'
,在這邊我們使用Realm這個套件
pod install
下載完之後,就可以來使用workspace。這個檔案包含了剛剛建立的Xcode專案、以及透過cocoapods下載的相依性類庫和文檔。
至於怎麼在mac裏面安裝cocoapods,我有點忘記我是怎麼做的,本來就有內建ruby,我好像是用
brew
:arrow_right: 什麼都用brew的me
然後我就在想,所以workspace跟project是有什麼差別ㄇ? 查到了以下3個的名詞解釋: Targets, projects, workspaces
Targets specify in detail how a product/binary (i.e., an application or library) is built. They include build settings, such as compiler and linker flags, and they define which files (source code and resources) actually belong to a product. When you build/run, you always select one specific target.
It is likely that you have a few targets that share code and resources. These different targets can be slightly different versions of an app (iPad/iPhone, different brandings,…) or test cases that naturally need to access the same source files as the app.
All these related targets can be grouped in a project. While the project contains the files from all its targets, each target picks its own subset of relevant files. The same goes for build settings: You can define default project-wide settings in the project, but if one of your targets needs different settings, you can always override them there.
In Xcode, you always open projects (or workspaces, but not targets), and all the targets it contains can be built/run, but there’s no way/definition of building a project, so every project needs at least one target in order to be more than just a collection of files and settings.
If, however, your library (the subproject) is used by a variety of other projects (or their targets, to be precise), it makes sense to put it on the same hierarchy level – that’s what workspaces are for. Workspaces contain and manage projects, and all the projects it includes directly (i.e., not their subprojects) are on the same level and their targets can depend on each other (projects’ targets can depend on subprojects’ targets, but not vice versa).
也就是說,專案(project)間的相依性使得需要一個更高階層的專案形式來管理,即workspace。
在這個workspace裡面,除了本身的專案,現在還多了pod這個專案,其中包含podfile引入的frameworks。
在to-do這個專案裡面,接下來會試著跟著mvc的架構,如下,進行實作
Model: a wrapper of data
View: a representation of a user interface (UI)
Controller: an intermediary between the Model and the View
UIViewController主要負責
每個app至少會有一個這樣的controller去處理畫面所要呈現的邏輯,而我們接下來的畫面邏輯會寫在這邊。
首先,把主要功能列出來,包含
在這個清單裡面,試想單個物件內容應該長怎樣? 除了todo名稱之外,還加入了其他細節、如日期,然後包成一個物件。
class TodoListItem: Object {
@objc dynamic var item: String = ""
@objc dynamic var date: Date = Date()
}
@objc dynamic
前綴使得接下來的東西可以在runtime時使用objctive-c dispatch。而之所以需要使用是因為我們會將物件資料儲存在Realm,根據官方文件我們將屬性加入前綴。
controller執行,將view load進記憶體後,所會執行的內容,通常我們會override掉初始值(所謂nib files)。
常見的幾個方法順序分別為:
到這邊可以先complie(cmd+B),也沒有新增什麼內容XD 基本上就是原本的初始專案,應該要編譯成功!
初始畫面,會以tableView的方式呈現,一項一項的清單。
下一篇會繼續實作!
若上述內容有誤或可以改進的部分,歡迎留言以及提出任何指教~
謝謝 ୧༼◕ ᴥ ◕༽୨